package org.wicket.jbpm.task.panel;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicket.jbpm.WicketJBPMSession;
import org.wicket.jbpm.engine.spring.JbpmCoreEngine;
import org.wicket.jbpm.engine.workflow.WfJbpmFactory;
import org.wicket.jbpm.engine.workflow.WfTask;

public class TaskListPanel extends Panel {

	@SpringBean	private JbpmCoreEngine jbpmEngine;
	
	private static final Log log = LogFactory.getLog(TaskListPanel.class);
	
	
	public TaskListPanel(String id) {
		super(id);
		
		final Form form = new Form("taskListForm",new CompoundPropertyModel(new WfTask()));
		add(form);
		
		final List<WfTask> taskList = jbpmEngine.getAssignedTasks(WicketJBPMSession.get().getUser().getCd());
		
		form.add(new ListView("listTask", taskList) {

			@Override
			protected void populateItem(ListItem item) {
				final WfTask wfTask = (WfTask)item.getModelObject();
				final Link link = new Link("taskLink") {
					@Override
					public void onClick() {
						try {
							setResponsePage(WfJbpmFactory.getTaskPage(wfTask.name, wfTask));
						} catch(Exception e) {
							e.printStackTrace();
						}
					}
				};				
				
				//link.add(new Image("openAppIcon",new ResourceReference(LOSCSS.class,"newApp.jpg")));
				link.add(new SimpleAttributeModifier("title","Open "+wfTask.id));
				item.add(link);
				item.add(new Label("lblStepName", wfTask.name));
				item.add(new Label("lblDescription", wfTask.description));
				item.add(new Label("lblCreateDt", wfTask.createDt.toString()));
				item.add(new Label("status", wfTask.state.toString()));
				
			}
				
		});
		
	}
}
